/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display;

import deatax.Tile;
import javax.media.opengl.GL2;

/**
 *
 * @author Caroline
 */
public class ForestTileRenderer extends TileRenderer {

    public static int TREE_COUNT = 10;
    public static float TREE_WIDTH = 2;
    public static float TREE_HEIGHT = 3;
    private static Texture GROUND_TEXTURE = new Texture("tiles/plains01.png");
    private static Texture TREE_TEXTURE = new Texture("tiles/tree.png");
    float[] tree_pos;

    public ForestTileRenderer() {
        addType("Forest");

        tree_pos = new float[TREE_COUNT * 20];
        for (int i = 0; i < TREE_COUNT*10; i++) {
            tree_pos[i * 2] = (float) Math.random() * getHexWidth() - getHexWidth() / 2 - TREE_WIDTH;
            tree_pos[i * 2 + 1] = (float) Math.random() * getHexHeight() - getHexHeight() / 2;
        }
    }

    @Override
    public void draw(GL2 gl, Tile t, float px, float py, float rotation) {
        super.draw(gl, t, px, py, rotation);

        GROUND_TEXTURE.bind(gl);
        gl.glEnable(GL2.GL_TEXTURE_2D);

        gl.glBegin(GL2.GL_TRIANGLE_FAN);
        for (int i = 0; i < 12; i += 2) {
            gl.glTexCoord2fv(hex_texture, i);
            gl.glVertex3f(hex_inner[i] + px, hex_inner[i + 1] + py, TILE_BEVEL);
        }
        gl.glEnd();
        gl.glDisable(GL2.GL_TEXTURE_2D);

    }

    @Override
    public void drawBorder(GL2 gl, Tile t, float px, float py, float rotation) {
        super.drawBorder(gl, t, px, py, rotation);
        
        gl.glColor3f(1,1,1);
        gl.glEnable(GL2.GL_TEXTURE_2D);
                TREE_TEXTURE.bind(gl);

//        gl.glDisable(GL2.GL_DEPTH_BUFFER);
//        gl.glDisable(GL2.GL_DEPTH_TEST);
        for (int i = 0; i < TREE_COUNT; i++) {
            drawTree(gl, px + tree_pos[Math.abs(Math.round(px) + i * 2)%tree_pos.length], py + tree_pos[Math.abs(Math.round(py) + i * 2 + 1)%tree_pos.length]);
        }

//        gl.glEnable(GL2.GL_DEPTH_BUFFER);
//        gl.glEnable(GL2.GL_DEPTH_TEST);

        gl.glDisable(GL2.GL_TEXTURE_2D);
    }
    
    

    public void drawTree(GL2 gl, float x, float y) {

        gl.glBegin(GL2.GL_TRIANGLES);
        gl.glTexCoord2f(1.1f, 1.1f);
        gl.glVertex3f(x - TREE_WIDTH/2, y, 0);
        gl.glTexCoord2f(0.5f, 0);
        gl.glVertex3f(x , y, TREE_HEIGHT);
        gl.glTexCoord2f(-0.1f, 1.1f);
        gl.glVertex3f(x + TREE_WIDTH/2, y, 0);

        gl.glTexCoord2f(1.1f, 1.1f);
        gl.glVertex3f(x, y+ TREE_WIDTH/2, 0);
        gl.glTexCoord2f(0.5f, 0);
        gl.glVertex3f(x , y, TREE_HEIGHT);
        gl.glTexCoord2f(-0.1f, 1.1f);
        gl.glVertex3f(x , y- TREE_WIDTH / 2, 0);
        gl.glEnd();

    }
}
